const {collectionsModel}= require('../models/collectionsModel')
const jwt = require("jsonwebtoken");

// 获取所有收藏
async function getAllCollection(req, res, next){
    let {user_id}=req.query;
    let collections = await collectionsModel.find({user_id}).populate("questions_id");
    // console.log(errorQuestions)
    res.send(collections)
}

// 取消收藏
async function deleCollection(req, res, next){
    console.log(req.body._id);
    let result = await collectionsModel.deleteMany({_id: req.body._id})
    console.log(result)
    if (result.deletedCount > 0) {
        res.send({ message: "删除成功", code: 200 })
    } else {
        res.send({ message: "删除失败", code: -1 })
    }
}
// 添加收藏
async function addCollection(req, res, next){
    // console.log(req.body.errorQuestions_id);
    let result = await collectionsModel.create({questions_id: req.body.errorQuestions_id,user_id:req.body.user_id})
    console.log(result)
    if (result) {
        res.send({ message: "添加成功", code: 200 })
    } else {
        res.send({ message: "添加失败", code: -1 })
    }
}
module.exports = {
    deleCollection,
    addCollection,
    getAllCollection
}